package com.ygd.day40encryptiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private String path = "http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";
    //毛泽东==%E6%AF%9B%E6%B3%BD%E4%B8%9C
    private String url1="https://www.baidu.com/s?word=%E6%AF%9B%E6%B3%BD%E4%B8%9C&tn=95853428_hao_pg&ie=utf-8&sc=UWY4PH6dn1fzr7qCmyqxTAThIjYkPj6snjn4nWbLn16vFhnqpA7EnHc1Fh7W5HbzPjR4nW6zPs&ssl_sample=s_4%2Cs_8&srcqid=437636421240459814";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
    }

    private void initData() {
        //URLEncoder编码
        String encode = null;
        try {
            encode = URLEncoder.encode("毛泽东","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "----------->initData: 进行URLEncode之后的内容" +encode);

        //使用URLDecoder进行解码
        //https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&tn=baidu&wd=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98&oq=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89&rsv_pq=b7171a8900006114&rsv_t=08f2GSjpAxo2Ez6eCJHWXhqOAijGhoRisb9WApE566yRs4v0MdZLFeGm%2B2k&rqlang=cn&rsv_enter=1&rsv_sug3=1&rsv_sug1=1&rsv_sug7=100&rsv_sug2=1&prefixsug=%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89&rsp=0&rsv_sug4=1120
        String str = "%E5%8C%97%E5%9B%BD%E9%A3%8E%E5%85%89%E5%8D%83%E9%87%8C%E5%86%B0%E5%B0%81%E4%B8%87%E9%87%8C%E9%9B%AA%E9%A3%98";
        String decoder = null;
        
        try {
            decoder = URLDecoder.decode(str,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Log.d("flag", "----------->inData:进行URLDecoder之后的内容是：" +decoder);

        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                ByteArrayOutputStream baos=null;

                try {

                    URL url=new URL(url1);

                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();

                    if (connection.getResponseCode()==200){

                        is = connection.getInputStream();
                        baos = new ByteArrayOutputStream();

                        byte[]buf=new byte[1024];

                        int len=0;

                        while ((len=is.read(buf))!=-1){
                            baos.write(buf,0,len);
                        }
                        byte[] bytes = baos.toByteArray();

                        Log.d("flag", "------------->:" + new String(bytes));

                        is.close();
                        baos.close();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
